#!/usr/local/python3/bin/python3
# -*- coding: UTF-8 -*-
import json
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import datetime

es_index = 'ipanalyzer'
es_type = 'patent'
json_path = "C:/Users/Administrator/Desktop/patent_analyzer_json_path.txt"


def is_none(data):
    if data is None:
        return True
    return False


# 日期格式化函数
def convert_date(date):
    date_str = date.replace('T', ' ')[0:19]
    return datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')


# 日期格式化函数,带毫秒
def convert_date_with_ms(date):
    date_str = date.replace('T', ' ')[0:23]
    return datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S.%f')


# 法律状态转换
def convert_legal_status(patent_legal_status_list):
    if is_none(patent_legal_status_list):
        return None
    patentLegalStatusesArray = []
    for i in range(len(patent_legal_status_list)):
        legal = patent_legal_status_list[i]
        legalDic = {
            "legalDate": convert_date(legal['legalDate']['$date']),
            "legalDescription": legal['legalDescription']
        }
        patentLegalStatusesArray.append(legalDic)
    return patentLegalStatusesArray


# 构造元数据
def build_patent_source(data):
    source = {}
    source['patentAbstract'] = data['patentAbstract']
    source['address'] = data['address']
    proxyOrg = data.get('proxyOrg', None)
    if is_none(proxyOrg) is False:
        source['proxyOrg'] = data['proxyOrg']
    proxyOrgCode = data.get('proxyOrgCode', None)
    if is_none(proxyOrgCode) is False:
        source['proxyOrgCode'] = data['proxyOrgCode']
    if 'proxyPerson' in data:
        if data['proxyPerson'] is not None:
            source['proxyPerson'] = data['proxyPerson']
    source['appCoun'] = data['appCoun']
    source['appDate'] = convert_date(data['appDate']['$date'])
    source['appNo'] = data['appNo']
    if 'appResource' in data:
        if data['appResource'] is not None:
            source['appResource'] = data['appResource']
    source['appPerson'] = data['appPerson']
    if 'expireDate' in data:
        if data['expireDate'] is not None:
            source['expireDate'] = convert_date(data['expireDate']['$date'])
    if 'grantDate' in data:
        if data['grantDate'] is not None:
            source['grantDate'] = convert_date(data['grantDate']['$date'])
    source['inventPerson'] = data['inventPerson']
    source['ipc'] = data['ipc']
    source['patentLegalStatuses'] = convert_legal_status(data.get('patentLegalStatuses', None))
    source['lprs'] = data['lprs']
    if 'pages' in data:
        if data['pages'] is not None:
            source['pages'] = int(data['pages'])
    source['patType'] = data['patType']
    source['patentType'] = data['patentType']
    source['pid'] = data['pid']
    if 'priority' in data:
        if data['priority'] is not None:
            source['priority'] = data['priority']
    if 'proCode' in data:
        if data['proCode'] is not None:
            source['proCode'] = data['proCode']
    source['publishDate'] = convert_date(data['publishDate']['$date'])
    source['publishNo'] = data['publishNo']
    source['statusCode'] = data['statusCode']
    source['patentName'] = data['patentName']
    source['insertTime'] = convert_date_with_ms(data['insertTime']['$date'])
    source['lastUpdateTime'] = convert_date_with_ms(data['lastUpdateTime']['$date'])
    return source


# 批量保存到es
def bulk_insert(array, s, e):
    try:
        helpers.bulk(es, array)
        print("Save success:%d to %d" % (s, e))
        return True
    except Exception:
        try:
            helpers.bulk(es, array)
            print("Save success:%d to %d" % (s, e))
            return True
        except Exception:
            try:
                helpers.bulk(es, array)
                print("Save success:%d to %d" % (s, e))
                return True
            except Exception:
                print("Save failed:%d to %d" % (s, e))
                return False


bulk_insert_num = 0
# 读取json文件路径
json_path_reader = open(json_path, encoding="utf-8")
file_path = json_path_reader.readline().strip()
if file_path == '':
    print('Please fill in the json file path')
else:
    print('Start reading json file : ', file_path)
    file = open(file_path.strip(), encoding="utf-8")
    line = file.readline()
    target_array = []

    # es连接获取
    es = Elasticsearch(hosts=[{'host': "192.168.1.167", 'port': "9200"}], timeout=30)

    while line:
        data = json.loads(line)
        # 获取json文件的数据
        source = build_patent_source(data)
        # es插入数据格式构建
        target = {"_index": es_index, "_type": es_type, "_id": data['_id']['$oid'], "_source": source}
        target_array.append(target)
        # 数组内部数量达到1000则往es插入一次
        if len(target_array) == 1000:
            start = bulk_insert_num
            bulk_insert_num = bulk_insert_num + 1000
            end = bulk_insert_num
            # bulk_insert(target_array, start, end)
            target_array.clear()

        line = file.readline()

    # 最后一次不足1000的内容插入es
    if len(target_array) > 0:
        start = bulk_insert_num
        bulk_insert_num = bulk_insert_num + len(target_array)
        end = bulk_insert_num
        # bulk_insert(target_array, start, end)
        target_array.clear()
    file.close()
    print('End reading')
json_path_reader.close()
